package com.jinniulive.platform.activity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;

import com.jinniulive.platform.R;
import com.jinniulive.platform.adapter.GuidePageViewPager;

import java.util.ArrayList;
import java.util.List;

public class GuidePageActivity extends AppCompatActivity implements ViewPager.OnPageChangeListener {
    private ViewPager mViewPager;
    private List<ImageView> mList;
    private Button mButton;
    private int [] ids =new int[]{R.mipmap.a,R.mipmap.a1,R.mipmap.a3};
    private GuidePageViewPager mAdapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_guide_page);
        initView();
        initData();
        setListener();
    }

    private void setListener() {
        // viewpager 监听
        mViewPager.addOnPageChangeListener(this);
    }

    private void initData() {
        mList=new ArrayList<>();
        for (int id : ids) {
            ImageView image =new ImageView(this);
            image.setImageResource(id);
            image.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                    ViewGroup.LayoutParams.MATCH_PARENT));
            image.setScaleType(ImageView.ScaleType.FIT_XY);
            mList.add(image);

        }
        mAdapter = new GuidePageViewPager(this,mList);
        mViewPager.setAdapter(mAdapter);

    }

    private void initView() {
        mViewPager = (ViewPager) findViewById(R.id.activity_guide_page_vp);
        mButton = (Button) findViewById(R.id.item_guide_paget_bt);
        SharedPreferences sharedPreferences = getSharedPreferences("isOne", MODE_PRIVATE);
        boolean is = sharedPreferences.getBoolean("is", false);
        if (is){
            Intent intent =new Intent(GuidePageActivity.this, StartPageActivity.class);
            startActivity(intent);
            finish();
        }
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
        if (position == 2){
            mButton.setVisibility(View.VISIBLE);
        }else {
            mButton.setVisibility(View.GONE);
        }
    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }

    public void onClick(View view) {
        Intent intent =new Intent(GuidePageActivity.this, StartPageActivity.class);
        startActivity(intent);
        finish();
    }
}
